/**
 * 
 */
package com.estock.platform.http.request;

import java.util.Map;

import com.estock.platform.http.utils.RequestUtil;


/**
 * @author richard
 *
 */
public class RequestParametersApplier {
	
	public BaseClientRequest applyParameters(BaseClientRequest message, Map<String, Object> params)
	        throws Exception {

        String messageUrl = message.getLocationUri();
        if (messageUrl != null) {
            boolean containsQuestionMark = messageUrl.contains("?");
            StringBuffer url = new StringBuffer(messageUrl);

            StringBuffer query = new StringBuffer(RequestUtil.format(params.entrySet(), "UTF-8"));

            if (!RequestUtil.isEmpty(query.toString())) {
                if (containsQuestionMark) {
                    url.append("&").append(query);
                } else {
                    url.append("?").append(query);
                }
            }

            message.setLocationUri(url.toString());
        }
        return message;
    }

}
